 #include <iostream>
#include <string>
#include <unordered_map>
#include <cmath>

bool is_prime(int num) {
    if (num < 2) {
        return false;
    }
    for (int i = 2; i <= sqrt(num); i++) {
        if (num % i == 0) {
            return false;
        }
    }
    return true;
}

int find_nearest_prime(std::string input_str) {
    std::unordered_map<std::string, int> numbers = {
        {"zero", 0}, {"one", 1}, {"two", 2}, {"three", 3}, {"four", 4},
        {"five", 5}, {"six", 6}, {"seven", 7}, {"eight", 8}, {"nine", 9}
    };

    std::string word1, word2, word3;
    std::istringstream iss(input_str);
    iss >> word1 >> word2 >> word3;

    if (iss.fail()) {
        return -1; // Wrong input
    }

    int num = 100 * numbers[word1] + 10 * numbers[word2] + numbers[word3];

    if (num < 100 || num > 999) {
        return -1; // Wrong input
    }

    int lower = num - 1;
    int upper = num + 1;

    while (!is_prime(lower) && !is_prime(upper)) {
        lower--;
        upper++;
    }

    if (is_prime(lower)) {
        return lower;
    } else {
        return upper;
    }
}

int main() {
    std::string input_str;
    std::cout << "Enter three words separated by spaces: ";
    std::getline(std::cin, input_str);

    int result = find_nearest_prime(input_str);
    if (result == -1) {
        std::cout << "Wrong input" << std::endl;
    } else {
        std::cout << result << std::endl;
    }

    return 0;
}
